<template>
  <div class="box">
    <div class="basic">
      <div class="info">
        <h1 class="name">{{ knowName }}</h1>
        <img src="@/assets/1.jpg" alt="" v-if="knowImg === ''">
        <img :src="knowImg" alt="" v-else>
      </div>
      <div class="setting">
        <el-button type="primary" class="share" plain @click="share()">
          <el-icon>
            <Share/>
          </el-icon>
          分享
        </el-button>
        <el-button type="primary" class="more" plain>
          <el-icon>
            <Edit/>
          </el-icon>
          重命名
        </el-button>
        <el-button type="primary" class="more" plain @click="openDelete(item)">
          <el-icon>
            <Delete/>
          </el-icon>
          删除
        </el-button>
      </div>
    </div>
    <div class="list-none" v-if="listVisible === false"><p>
      文库内暂无内容
    </p></div>
    <div class="list" v-else>
      <div class="every" v-for="(item,index) in list" :key="item.id" @click="enterWrite(item.id)">
        <div class="title">
          <el-icon>
            <Document/>
          </el-icon>
          <p>{{ item.name }}</p>
        </div>
        <div style="border: 1px dashed #ccc;flex: 1;"></div>
        <p class="time">{{ item.createTime }}</p>
      </div>
    </div>

    <!--    分享弹窗-->
    <el-dialog v-model="shareVisible" title="分享" width="500">
      <el-form>
        <el-form-item label="设置有效期：">
          <el-radio-group v-model="settingType">
            <el-radio :value="7">7天有效</el-radio>
            <el-radio :value="30">30天有效</el-radio>
            <el-radio :value="100">永久有效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="链接分享：">
          <el-input v-model="linkName" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="设置编辑：">
          <el-switch v-model="range" :change="editChange"/>
        </el-form-item>
        <el-form-item label="" v-show="range === true">
          <div style="width: 500px;display: flex;justify-content: space-between;align-items: center">
            <el-input v-model="editPassword" autocomplete="off" style="margin-right: 20px"/>
            <el-button type="primary" @click="getCode()">生成编辑码</el-button>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="stopShare()">停止分享</el-button>
        </div>
      </template>
    </el-dialog>

    <!--  删除确认框-->
    <el-dialog
        v-model="deleteVisible"
        title="提示"
        width="500"
    >
      <span>确定要删除文库{{ knowName }}?</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="deleteVisible = false">取消</el-button>
          <el-button type="primary" @click="sureDelete()">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!--  重命名弹出框-->
    <el-dialog v-model="renameVisible" title="重命名" width="500">
      <el-form>
        <el-form-item label="文库名称">
          <el-input v-model="newName" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="renameVisible = false">取消</el-button>
          <el-button type="primary" @click="rename()">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>
<script setup>
import {onMounted, ref, watch} from "vue";
import router from "@/router/index.js";
import Api from '@/api/index.ts'
import {ElMessage} from "element-plus";
import {onBeforeRouteUpdate, useRoute} from "vue-router";

const id = ref()

onMounted(() => {
  id.value = route.query.id;
  getTree(id.value)
})

const route = useRoute();
watch(() => route.query.id, (newValue, oldValue) => {
  id.value = newValue;
})

onBeforeRouteUpdate((to, from, next) => {
  id.value = to.query.id;
  getTree(id.value)
  next()
})


const enterWrite = (id) => {
  router.push({path: '/write', query: {id: id, source: 'detail'}})
}

// 获取目录下的所有文件
const knowName = ref();
const knowImg = ref('');
const listVisible = ref(false)
const list = ref([])
const getTree = (id) => {
  Api.getTree(id).then((res) => {
    if (res.code === 1) {
      ElMessage({
        message: '获取知识库下的所有文件成功',
        type: 'success',
      })
      knowName.value = res.data[0].name;
      if (res.data[0].color != null) {
        knowImg.value = res.data[0].color;
      }
      if (res.data[0].children.length > 0) {
        listVisible.value = true;
        list.value = res.data[0].children;
      }
    } else {
      ElMessage({
        message: '获取知识库下的所有文件失败',
        type: 'error',
      })
    }
  })
}

// 删除
const deleteVisible = ref(false);

const openDelete = () => {
  deleteVisible.value = true
}

const sureDelete = () => {
  deleteVisible.value = false;
  Api.deleteHouse(id.value).then((res) => {
    if (res.code === 1) {
      ElMessage({
        message: '删除文库' + knowName.value + '成功',
        type: 'success',
      })
      router.push({path: '/know'})
    } else {
      ElMessage.error('删除文库' + knowName.value + '失败')
    }
  })
}

// 重命名弹出框
const renameVisible = ref(false);
const newName = ref('');

const openRename = (item) => {
  renameVisible.value = true
  newName.value = item;
}

const rename = () => {
  renameVisible.value = false
  Api.setInfo(newName.value).then((res) => {
    if (res.code === 1) {
      ElMessage({
        message: '重命名文库成功',
        type: 'success',
      })
      if (flag.value === false) {
        getAll()
      } else {
        getOwn()
      }
    } else {
      ElMessage.error('重命名文库失败')
    }
  })
}


// 分享
const shareVisible = ref(false)
const settingType = ref(100)
const linkName = ref('')
const range = ref(false)
const type = ref('CREATE_URL')
const editPassword = ref('')

const editChange = () => {
  range.value = !range.value
  const data = {id: id.value, settingType: type.value, share: true, validityType: 'SEVEN_DAY'}
  if (type.value === 'PWD') {
    data.pwd = editPassword.value;
    Api.setShare(data).then(res => {
      console.log(res)
      shareFlag.value = false
    })
  } else {
    Api.setShare(data).then(res => {
      console.log(res)
      shareFlag.value = false
    })
  }
}

const shareFlag = ref(true)
const share = () => {
  shareVisible.value = true
  const data = {id: id.value, settingType: type.value, share: true, validityType: 'SEVEN_DAY'}
  if (type.value === 'PWD') {
    data.pwd = editPassword.value;
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        linkName.value = res.data.shareKey;
        ElMessage.success("分享成功")
      }else{
        ElMessage.error("分享失败")
      }
      shareFlag.value = false
    })
  } else {
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        linkName.value = res.data.shareKey
        ElMessage.success("分享成功")
      }else{
        ElMessage.error("分享失败")
      }
      shareFlag.value = false
    })
  }
}

// 生成密码后重新发起请求
const getCode = () => {
  const data = {id: id.value, settingType: type.value, share: true, validityType: 'SEVEN_DAY'}
  if (type.value === 'PWD') {
    data.pwd = editPassword.value;
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        ElMessage.success("分享成功")
        linkName.value = res.data.shareKey
      }else{
        ElMessage.error("分享失败")
      }
      shareFlag.value = false
    })
  } else {
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        ElMessage.success("分享成功")
        linkName.value = res.data.shareKey
      }else{
        ElMessage.error("分享失败")
      }
      shareFlag.value = false
    })
  }
}

// 停止分享
const stopShare = () => {
  const data = {id: id.value, settingType: type.value, share: false, validityType: 'SEVEN_DAY'}
  if (type.value === 'PWD') {
    data.pwd = editPassword.value;
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        ElMessage({
          message: '停止分享成功',
          type: 'success',
        })
      }
      shareFlag.value = false
    })
  } else {
    Api.setShare(data).then(res => {
      if (res.code === 1) {
        ElMessage({
          message: '停止分享成功',
          type: 'success',
        })
      }
      shareFlag.value = false
    })
  }
}

</script>
<style scoped lang="less">
.box {
  width: 80%;
  height: 82vh;
  background-color: #fff;
  margin: 20px auto;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.3);

  .basic {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .info {
      display: flex;
      align-items: center;
      justify-content: flex-start;

      .name {
        font-size: 26px;
        font-weight: bold;
        margin-right: 20px;
      }

      img {
        width: 40px;
        height: 40px;
        border-radius: 5px;
      }
    }

  }

  .list-none {
    margin-top: 20%;
    padding: 10px 0;
    text-align: center;
    font-size: 14px;
  }

  .list {
    margin-top: 30px;
    padding: 10px 0;

    .every {
      display: flex;
      flex-direction: row;
      justify-content: space-evenly;
      align-items: center;
      margin: 10px 0;
      cursor: pointer;

      .title {
        display: flex;
        justify-content: flex-start;
        align-items: center;

        p {
          margin: 0 6px;
          font-size: 18px;
        }
      }

      .time {
        font-size: 12px;
        margin: 0 10px;
        color: #aaa;
      }
    }
  }
}
</style>
